package com.iflytek.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.iflytek.entity.Dept;
import com.iflytek.entity.User;
import com.iflytek.service.DeptService;
import com.iflytek.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

//用户管理模块的控制器
@Controller
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService departmentService;

//    @GetMapping("/userList")
//    public String listUser(Model model) {
//        List<User> userList = userService.findAllUser();
//        model.addAttribute("userList", userList);
//        System.out.println(userList.get(0).getName() + "==========");
//        return "userList";
//    }

    @GetMapping("/userDetail")
    //SpringBoot提供的一个Model对象，是Controller和页面之间的一个桥梁
    //要使用Model，直接在方法上声明Model就可以使用
    public String showUserDetail(Model model, int id) {
        //获取页面所需要的数据
//        String name = "老王";
//        int age = 18;
        //往model中放入变量
//        model.addAttribute("username", name);
//        model.addAttribute("age", age);
        User user = userService.getById(id);
        model.addAttribute("user", user);
        return "userDetail";
    }

    @GetMapping("/createUser")
    public String toCreateUser(Model model) {
        List<Dept> departmentList = departmentService.list();
        model.addAttribute("departmentList", departmentList);
        return "userCreate";
    }

    @PostMapping("/saveUser")
    public String createUser(User user, Model model) {
        try {
            userService.saveUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", e.getMessage());
            return "userCreate";
        }

        return "redirect:userList";
    }

    @GetMapping("/userDelete")
    public String delUser(Integer id) {
        userService.removeById(id);
        return "redirect:/userList";
    }

    @GetMapping("/userMark")
    public String markUser(Integer id) {
        User user = userService.getById(id);
        QueryWrapper<User> w = new QueryWrapper<User>().eq("id", id);;
        user.setDel(1);
        userService.update(user, w);
        return "redirect:/userList";
    }

    @GetMapping("/userEdit")
    public String editUser(Integer id, Model model) {
        User user = userService.getById(id);
        model.addAttribute("user", user);

        List<Dept> departmentList = departmentService.list();
        model.addAttribute("departmentList", departmentList);
        return "userEdit";
    }

    @PostMapping("/userUpdate")
    public String updateUser(User user, Model model) {
        userService.updateById(user);
        String msg = "修改成功";
        model.addAttribute("msg", msg);

        List<Dept> departmentList = departmentService.list();
        model.addAttribute("departmentList", departmentList);
        return "userEdit";
    }

    //批量删除
    @GetMapping("/catchDelUser")
    public String catchDelUser(Integer[] ids, Model model) {
        userService.catchMark(ids);

        return "redirect:userList";
    }


    //模糊查询按参数查询
    @GetMapping(value = {"/queryUser", "/userList"})
    public String getUserByParams(@ModelAttribute("params") User params, Model model, @RequestParam(defaultValue = "5") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) {
        System.out.println(params);
        PageInfo<User> pageInfo = userService.findByParams(params, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);

        List<Dept> departmentList = departmentService.list();
        model.addAttribute("dept", departmentList);
        return "userList";
    }


}
